package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.PupilData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PupiDataDaoImpl extends TempDataObjectDao<PupilData> {
    public static String getPupilDataSql = "select * from `t_u_pupil` where `userId`=?";
    public Map<Integer, Map<Integer, PupilData>> getPupilData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, PupilData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPupilDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                PupilData data = parse(rs);
                int patronsId = data.getPatronsId();
                if (!dataMap.containsKey(patronsId)) {
                    dataMap.put(patronsId, new ConcurrentHashMap<>());
                }
                Map<Integer, PupilData> pupilDataMap = dataMap.get(patronsId);
                pupilDataMap.put(data.getTitle(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updatePupilDataSql = "update `t_u_pupil` set num=? where userId=? and patronsId=? and title = ?";
    @Override
    protected boolean updateData(PupilData pupilData) {
        pupilData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, pupilData.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, pupilData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, pupilData.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, pupilData.getTitle()));
        if (getDb().executeNoneQuery(updatePupilDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPupilDataSql = "insert into `t_u_pupil`(userId,patronsId,title,num) values (?,?,?,?)";
    @Override
    protected boolean addData(PupilData pupilData) {
        pupilData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, pupilData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, pupilData.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, pupilData.getTitle()));
        para.put(i++, new DbParameter(Types.INTEGER, pupilData.getNum()));
        if (getDb().executeNoneQuery(insertPupilDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PupilData parseData(ResultSet rs) throws SQLException {
        PupilData pupilData = new PupilData();
        pupilData.setUserId(rs.getLong("userId"));
        pupilData.setPatronsId(rs.getInt("patronsId"));
        pupilData.setTitle(rs.getInt("title"));
        pupilData.setNum(rs.getInt("num"));

        return pupilData;
    }
}
